package cn.ihuizi.action;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ihuizi.dao.RoleDao;
import cn.ihuizi.dao.impl.RoleDaoImpl;
import cn.ihuizi.entity.Role;

/**
 * Servlet implementation class RoleSelectServlet
 */
@WebServlet("/RoleSelectServlet")
public class RoleSelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RoleDao roleDao = new RoleDaoImpl();
		ResultSet rs = roleDao.selectAllRole();
		List<Role> list = new ArrayList();
		try {
			while(rs.next()) {
				int rid = rs.getInt("role_id");
				String rname = rs.getString("role_name");
				
				
				List listpower = new ArrayList();
				ResultSet rs1= roleDao.selectPowerByRoleid(new Role(rid));
				while(rs1.next()) {
					String pname = rs1.getString("power_name");
					listpower.add(pname);
				}
				Role role = new Role(rid,rname,listpower);
				list.add(role);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("role", list);
		request.getRequestDispatcher("view/role/role_list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
